//
//  BVLoginViewController.m
//  XiangYu
//
//  Created by YYKJ on 2017/6/5.
//  Copyright © 2017年 YYKJ. All rights reserved.
//

#import "BVLoginViewController.h"
#import "BVLoginAndRegisTF.h"
#import "BVResetPwdViewController.h"
#import "BVRegisViewController.h"
#import "BVTabBarController.h"
#import "BVMineTool.h"

@interface BVLoginViewController ()<UINavigationControllerDelegate>

@property (nonatomic ,strong) UITextField *accountTF;
@property (nonatomic ,strong) UITextField *pwdTF;
@property (nonatomic ,strong) BVMineTool  *mineTool;

@end

@implementation BVLoginViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    self.navigationController.delegate = self;
    
}
- (void)navigationController:(UINavigationController *)navigationController
      willShowViewController:(UIViewController *)viewController
                    animated:(BOOL)animated {
    
    BOOL isShowHomePage = [viewController isKindOfClass:[self class]];
    [self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
    
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.view endEditing:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor BVBackgroundLightGrayColor]];
    
    [self layouUI];
}

-(void)layouUI{
    
    UIImageView *img = [[UIImageView alloc]init];
    [self.view addSubview:img];
    [img setImage:[UIImage imageNamed:@"login_image"]];
    img.sd_layout
    .topSpaceToView(self.view, 60)
    .centerXIs(ScreenW/2)
    .heightIs(160)
    .widthIs(320);
    
    self.accountTF = [self textFieldWithText:@"请输入您的手机号" imageName:@"shouji"];
    [self.accountTF setKeyboardType:UIKeyboardTypePhonePad];
    self.accountTF.sd_layout
    .leftSpaceToView(self.view, 30)
    .rightSpaceToView(self.view, 30)
    .topSpaceToView(img, 0)
    .heightIs(45);
    
    self.pwdTF = [self textFieldWithText:@"请输入您密码" imageName:@"lock"];
    self.pwdTF.sd_layout
    .leftSpaceToView(self.view, 30)
    .rightSpaceToView(self.view, 30)
    .topSpaceToView(self.accountTF, 20)
    .heightIs(45);
    
    UIButton *loginBtn = [self buttonWithTitle:@"登录" withTag:0];
    [loginBtn setBackgroundColor:[UIColor BVBaseOrangeColor]];
    [loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginBtn addTarget:self action:@selector(loginAndRegis:) forControlEvents:UIControlEventTouchUpInside];
    loginBtn.sd_layout
    .leftSpaceToView(self.view, 30)
    .rightSpaceToView(self.view, 30)
    .topSpaceToView(self.pwdTF, 30)
    .heightIs(45);
    
    UIButton *regisBtn = [self buttonWithTitle:@"注册账号" withTag:100];
    [regisBtn setTag:100];
    regisBtn.sd_layout
    .topSpaceToView(loginBtn, 10)
    .leftEqualToView(self.pwdTF)
    .heightIs(30)
    .widthIs(80);
    
    UIButton *resetBtn = [self buttonWithTitle:@"忘记密码" withTag:200];
    resetBtn.sd_layout
    .topSpaceToView(loginBtn, 10)
    .rightEqualToView(self.pwdTF)
    .heightIs(30)
    .widthIs(80);
    
}

-(void)regisAndResetBtnClick:(UIButton *)btn{
    
    if (btn.tag == 100) {
        BVRegisViewController *regis = [[BVRegisViewController alloc]init];
        ROOTNAVPUSH(regis, YES);
    }else if (btn.tag == 200){
        BVResetPwdViewController *reset = [[BVResetPwdViewController alloc]init];
        ROOTNAVPUSH(reset, YES);
    }
}

-(void)loginAndRegis:(UIButton *)btn{
    
    if (![NSString valiMobile:self.accountTF.text]) {
        [self showWarning:@"手机号格式错误"];
    }else{
        if (self.pwdTF.text.length <= 0) {
            [self showWarning:@"密码格式不正确"];
        }else{
            [self.hud show:YES];
            [self.mineTool loginTel:self.accountTF.text password:self.pwdTF.text success:^(NSString *status) {
                [self.hud hide:YES];
                [self showSuccess:status];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    BVTabBarController *webView = [[BVTabBarController alloc]init];
                    BVKeyWindow.rootViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
                    [UIView transitionWithView:BVKeyWindow
                                      duration:0.3f
                                       options:UIViewAnimationOptionTransitionCrossDissolve
                                    animations:nil
                                    completion:nil];
                    BVKeyWindow.rootViewController = webView;
                });
            } codeFailure:^(NSString *str) {
                [self.hud hide:YES];
                [self showError:str];
            } failure:^(NSError *error) {
                [self.hud hide:YES];
                [self showError:@"网络错误"];
            }];
        }
    }
}

-(UITextField *)textFieldWithText:(NSString *)text imageName:(NSString *)imageName{
    
    UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 45, 45)];
    [imgView setImage:[UIImage imageNamed:imageName]];
    [imgView setContentMode:UIViewContentModeCenter];
    UITextField *textField = [[UITextField alloc]init];
    [self.view addSubview:textField];
    [UIView makeCorner:1.0 view:textField color:[UIColor BVBaseOrangeColor]];
    [UIView makeCorner:5.0 view:textField];
    [textField setPlaceholder:text];
    [textField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];
    [textField setLeftView:imgView];
    [textField setLeftViewMode:UITextFieldViewModeAlways];
    [textField setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
    return textField;
}

-(UIButton *)buttonWithTitle:(NSString *)title withTag:(NSInteger )tag{
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:btn];
    [btn setTitleColor:[UIColor BVTextDarkGrayColor] forState:UIControlStateNormal];
    [btn.titleLabel setFont:[UIFont systemFontOfSize:16]];
    [UIView makeCorner:5.0 view:btn];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(regisAndResetBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [btn setTag:tag];
    return btn;
}

-(BVMineTool *)mineTool{
    if (!_mineTool) {
        _mineTool = [[BVMineTool alloc]init];
    }
    return _mineTool;
}

-(void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
